package com.open.exam.mapper;

import com.open.exam.databasemodel.*;
import com.open.exam.sqlprovider.ExamineeProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 奶茶
 * Created on 2017/11/16.
 */
@Mapper
public interface ExamineeMapper {

    @SelectProvider(type = ExamineeProvider.class,method = "findExamineesByCardNo")
    List<Examinee> findExamineesByCardNo(@Param(value = "examCardNo") String examCardNo);

    @Select("SELECT * FROM examinee WHERE ExamCardNO = #{examCardNo} AND password = #{passWord} LIMIT 1")
    Examinee getExaminerByCardNo(@Param(value = "examCardNo") String examCardNo, @Param(value = "passWord") String passWord);

    @Select("SELECT * FROM Examinee WHERE AppID = #{appID} AND ExamPlanInfoID = #{examPlanInfoID} AND ExamineeID = #{examineeID}")
    List<Examinee> findExamineeList(@Param(value = "examineeID") String examineeID, @Param(value = "examPlanInfoID") String examPlanInfoID, @Param(value = "appID") String appID);

    @Insert("INSERT INTO Examinee (\n" +
            "\tAppID,ExamSiteID,ExamPlanInfoID,ExamineeID,ExamineeName,ExamCardNo,PASSWORD,Sex,CertificateType,PhotoResource,CertificateNo,AccountType\n" +
            ") SELECT #{appID},#{examSiteID},#{examPlanInfoID},#{examineeID},#{examineeName},#{examCardNo},#{password},#{sex},#{certificateType},#{photoResource},#{certificateNo},#{accountType}\n" +
            "FROM\n" +
            "\tDUAL\n" +
            "WHERE NOT EXISTS (\n" +
            "\t\tSELECT\n" +
            "\t\t\t1\n" +
            "\t\tFROM\n" +
            "\t\t\tExaminee\n" +
            "\t\tWHERE\n" +
            "\t\t\tExamPlanInfoID = #{examPlanInfoID}\n" +
            "\t\tAND ExamineeID = #{examineeID}\n" +
            "\t\tLIMIT 1\n" +
            ")")
    Integer insertExaminee(Examinee examinee);

    @Insert("INSERT INTO Examinee (AppID,ExamSiteID,ExamPlanInfoID,ExamineeID,ExamineeName,ExamCardNo,Sex,CertificateType,PhotoResource,CertificateNo,AccountType,password) VALUES (#{appID},#{examSiteID},#{examPlanInfoID},#{examineeID},#{examineeName},#{examCardNo},#{sex},#{certificateType},#{photoResource},#{certificateNo},#{accountType},#{password})")
    Integer insertExaminer(Examinee examinee);

    @Update("UPDATE Examinee SET ExamineeName = #{examineeName},ExamCardNo = #{examCardNo},Sex = #{sex},CertificateType = #{certificateType},CertificateNo = #{certificateNo},AccountType = #{accountType} WHERE AppID = #{appID} AND ExamSiteID = #{examSiteID} AND ExamPlanInfoID = #{examPlanInfoID} AND ExamineeID = #{examineeID}")
    Integer updateExaminee(Examinee examinee);

    @Delete("DELETE FROM Examinee WHERE ExamPlanInfoID = #{planId}")
    void delExamineeByPlanId(@Param("planId") String planId);

    @Select("SELECT * FROM Examinee WHERE ExamineeID = #{examineeID} LIMIT 1")
    Examinee findExamineeInfo(@Param("examineeID") String examineeID);

    @SelectProvider(type = ExamineeProvider.class , method = "findExamClientData")
    List<Examinee> findExamClientExaminee(@Param("tableName") String tableName, @Param("examineeId") String examineeId,
                                          @Param("serviceTime") String serviceTime, @Param("testPaperCreated") Integer testPaperCreated,
                                          @Param("submited") Integer submited, @Param("appId") String appId, @Param("planId") String planId,
                                          @Param("subjectId") String subjectId, @Param("examCardNo") String examCardNo);

    @SelectProvider(type = ExamineeProvider.class , method = "findExamClientData")
    List<ExamineeArrange> findExamClientExamineeArrange(@Param("tableName") String tableName, @Param("examineeId") String examineeId,
                                                        @Param("serviceTime") String serviceTime, @Param("testPaperCreated") Integer testPaperCreated,
                                                        @Param("submited") Integer submited, @Param("appId") String appId, @Param("planId") String planId,
                                                        @Param("subjectId") String subjectId, @Param("examCardNo") String examCardNo);

    @SelectProvider(type = ExamineeProvider.class , method = "findExamClientData")
    List<ExamingMonitor> findExamClientExamingMonitor(@Param("tableName") String tableName, @Param("examineeId") String examineeId,
                                                      @Param("serviceTime") String serviceTime, @Param("testPaperCreated") Integer testPaperCreated,
                                                      @Param("submited") Integer submited, @Param("appId") String appId, @Param("planId") String planId,
                                                      @Param("subjectId") String subjectId, @Param("examCardNo") String examCardNo);

    @SelectProvider(type = ExamineeProvider.class , method = "findExamClientData")
    List<ExamPlanInfo> findExamClientExamPlanInfo(@Param("tableName") String tableName, @Param("examineeId") String examineeId,
                                                  @Param("serviceTime") String serviceTime, @Param("testPaperCreated") Integer testPaperCreated,
                                                  @Param("submited") Integer submited, @Param("appId") String appId, @Param("planId") String planId,
                                                  @Param("subjectId") String subjectId, @Param("examCardNo") String examCardNo);

    @SelectProvider(type = ExamineeProvider.class , method = "findExamClientData")
    List<ExamRoom> findExamClientExamRoom(@Param("tableName") String tableName, @Param("examineeId") String examineeId,
                                          @Param("serviceTime") String serviceTime, @Param("testPaperCreated") Integer testPaperCreated,
                                          @Param("submited") Integer submited, @Param("appId") String appId, @Param("planId") String planId,
                                          @Param("subjectId") String subjectId, @Param("examCardNo") String examCardNo);

    @SelectProvider(type = ExamineeProvider.class , method = "findExamClientData")
    List<ExamScenes> findExamClientExamScenes(@Param("tableName") String tableName, @Param("examineeId") String examineeId,
                                              @Param("serviceTime") String serviceTime, @Param("testPaperCreated") Integer testPaperCreated,
                                              @Param("submited") Integer submited, @Param("appId") String appId, @Param("planId") String planId,
                                              @Param("subjectId") String subjectId, @Param("examCardNo") String examCardNo);

    @SelectProvider(type = ExamineeProvider.class , method = "findExamClientData")
    List<ExamSite> findExamClientExamSite(@Param("tableName") String tableName, @Param("examineeId") String examineeId,
                                          @Param("serviceTime") String serviceTime, @Param("testPaperCreated") Integer testPaperCreated,
                                          @Param("submited") Integer submited, @Param("appId") String appId, @Param("planId") String planId,
                                          @Param("subjectId") String subjectId, @Param("examCardNo") String examCardNo);

    @SelectProvider(type = ExamineeProvider.class , method = "findExamClientData")
    List<ExamSubject> findExamClientExamSubject(@Param("tableName") String tableName, @Param("examineeId") String examineeId,
                                                @Param("serviceTime") String serviceTime, @Param("testPaperCreated") Integer testPaperCreated,
                                                @Param("submited") Integer submited, @Param("appId") String appId, @Param("planId") String planId,
                                                @Param("subjectId") String subjectId, @Param("examCardNo") String examCardNo);

    @Select("SELECT * FROM Examinee WHERE ExamineeID = #{examineeId} LIMIT 1")
    Examinee findFirstExamineesByExamineeId(@Param("examineeId") String examineeId);

    @Select("SELECT * FROM Examinee WHERE ExamCardNo = #{examCardNo} AND CertificateNo = #{idCardNo}")
    List<Examinee> findExamineesByCardNoAndCertificateNo(@Param("examCardNo") String examCardNo, @Param("idCardNo") String idCardNo);

    @Select("SELECT * FROM Examinee WHERE ExamCardNo = #{examCardNo} AND ExamPlanInfoID = #{planId} LIMIT 1")
    Examinee findExamineesByCardNoAndPlanId(@Param("examCardNo") String examCardNo, @Param("planId") Integer planId);
}
